﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace FanaticRock.Theme.Helpers
{
    ///<summary>图片质量。</summary>
    public enum ImageQuality
    {
        VeryLow, Low, ForCard, Standard, High, Max
    }

    ///<summary>图片相关操作</summary>
    public static class ImageHelper
    {
        private static int GetImageQualityWidth(ImageQuality quality)
        {
            var decodeWidth = 160;
            switch (quality)
            {
                case ImageQuality.VeryLow:
                    decodeWidth = 80;
                    break;
                case ImageQuality.Low:
                    decodeWidth = 160;
                    break;
                case ImageQuality.ForCard:
                    decodeWidth = 320;
                    break;
                case ImageQuality.Standard:
                    decodeWidth = 480;
                    break;
                case ImageQuality.High:
                    decodeWidth = 960;
                    break;
                case ImageQuality.Max:
                    decodeWidth = 0;
                    break;
                default:
                    break;
            }

            return decodeWidth;
        }

        public static BitmapImage CreateImageSource(byte[] bytes, ImageQuality quality = ImageQuality.Standard)
        {
            if (bytes == null) return null;
            var imageSource = new BitmapImage();
            var stream = new MemoryStream(bytes);
            imageSource.BeginInit();
            imageSource.CacheOption = BitmapCacheOption.OnLoad;
            imageSource.CreateOptions = BitmapCreateOptions.DelayCreation;
            imageSource.StreamSource = stream;
            int decodeWidth = GetImageQualityWidth(quality);
            imageSource.DecodePixelWidth = decodeWidth;
            imageSource.EndInit();
            imageSource.Freeze();
            stream.Close();
            return imageSource;
        }



        public static BitmapImage CreateImageSource(string uri, ImageQuality quality = ImageQuality.Standard)
        {
            if (string.IsNullOrWhiteSpace(uri)) return null;
            if (File.Exists(uri) == false) return null;
            var imageSource = new BitmapImage();
            imageSource.BeginInit();
            imageSource.CacheOption = BitmapCacheOption.OnLoad;
            imageSource.CreateOptions = BitmapCreateOptions.DelayCreation;
            imageSource.UriSource = new Uri(uri);
            int decodeWidth = GetImageQualityWidth(quality);
            imageSource.DecodePixelWidth = decodeWidth;
            imageSource.EndInit();
            imageSource.Freeze();
            return imageSource;
        }

        public static BitmapImage CreateImageSource(Uri uri, ImageQuality quality = ImageQuality.Standard)
        {
            var imageSource = new BitmapImage();
            imageSource.BeginInit();
            imageSource.CacheOption = BitmapCacheOption.OnLoad;
            imageSource.CreateOptions = BitmapCreateOptions.DelayCreation;
            imageSource.UriSource = uri;
            int decodeWidth = GetImageQualityWidth(quality);
            imageSource.DecodePixelWidth = decodeWidth;
            imageSource.EndInit();
            imageSource.Freeze();
            return imageSource;
        }

        public static ImageBrush CreateImageBrush(byte[] bytes)
        {
            return CreateImageBrush(CreateImageSource(bytes));
        }

        public static ImageBrush CreateImageBrush(ImageSource imageSource)
        {
            return new ImageBrush(imageSource);
        }

        public static byte[] ExtractBytes(ImageSource imageSource)
        {
            if (imageSource == null) return null;
            if (imageSource is BitmapImage)
            {
                return BitmapImageToByteArray(imageSource as BitmapImage);
            }
            return null;
        }

        public static byte[] BitmapImageToByteArray(BitmapImage bmp)
        {
            byte[] bytearray = null;
            try
            {
                using (Stream smarket = bmp.StreamSource)
                {
                    if (smarket != null && smarket.Length > 0)
                    {
                        //设置当前位置
                        smarket.Position = 0;
                        using (BinaryReader br = new BinaryReader(smarket))
                        {
                            bytearray = br.ReadBytes((int)smarket.Length);
                        }
                    }
                    else if (bmp.UriSource != null)
                    {
                        return File.ReadAllBytes(bmp.UriSource.LocalPath);
                    }
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            return bytearray;
        }

    }
}
